﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public static class PanelManager{
	public static Dictionary<string, BasePanel> panels = new Dictionary<string, BasePanel>();

	private static BasePanel currShowPanel;

	public static T Get<T>() where T:BasePanel
	{
		string name = typeof(T).ToString();
		BasePanel panel;
		if (!panels.TryGetValue(name, out panel))
		{
			panel = (T)Activator.CreateInstance(typeof(T));
			panel.Init();
			panels.Add(name, panel);
		}
		return (T)panel;
	}

	public static T Open<T>(params object[] para) where T : BasePanel
	{
		BasePanel panel = Get<T>();
		if (currShowPanel != null)
		{
			currShowPanel.Close();
		}
		panel.Show(para);
		currShowPanel = panel;
		return (T)panel;
	}

	public static void Dispose(string name)
    {
		if (!panels.ContainsKey(name))
		{
			return;
		}
		BasePanel panel = panels[name];
		panels.Remove(name);
		panel.Dispose();
	}
}
